package com.geek.gkfood.web.aspect;

import com.geek.gkfood.biz.util.RespUtils;
import com.geek.gkfood.facade.abs.AbstractResponse;
import com.geek.gkfood.util.excp.GkFoodCodeEnum;
import com.geek.gkfood.util.excp.GkFoodException;
import lombok.extern.slf4j.Slf4j;
import org.springframework.core.annotation.Order;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.ResponseBody;

/**
 * <h1>Web 全局异常拦截处理。</h1><br/>
 *
 * <h1><br/><a href="https://u.geekbang.org/subject/arch3/100614201">Java 业务架构实战营：https://u.geekbang.org/subject/arch3/100614201</a></h1><br/><h1><a href="https://gitee.com/ylimhhmily/BizArchPracticeCamp">Java 业务架构实战营代码地址：https://gitee.com/ylimhhmily/BizArchPracticeCamp</a></h1><br/><h1><a href="https://u.geekbang.org/subject/java4th/1001148?source=app_share">高级 Java 工程体系课 2023 版：https://u.geekbang.org/subject/java4th/1001148?source=app_share</a></h1><br/><h1><a href="https://time.geekbang.org/column/intro/100312101?tab=catalog">Dubbo 源码剖析与实战：https://time.geekbang.org/column/intro/100312101?tab=catalog</a></h1>
 *
 * @author hmilyylimh
 * ^_^
 * @version 0.0.1
 * ^_^
 * @date 2023/9/27 21:06
 */
@ControllerAdvice
@Slf4j
@Order(Integer.MIN_VALUE + 10000)
public class WebGlobalExceptionHandler {

    @ExceptionHandler(value = Throwable.class)
    @ResponseBody
    public AbstractResponse handleIThrowable(Throwable e) {
        AbstractResponse resp = new AbstractResponse();
        return RespUtils.fail(resp, GkFoodCodeEnum.SYSTEM_EXCEPTION);
    }

    @ExceptionHandler(value = GkFoodException.class)
    @ResponseBody
    public AbstractResponse handleGkFoodException(GkFoodException e) {
        AbstractResponse resp = new AbstractResponse();
        return RespUtils.fail(resp, e.getExcpCode(), e.getExcpMemo());
    }

    @ExceptionHandler(value = IllegalArgumentException.class)
    @ResponseBody
    public AbstractResponse handleIllegalArgumentException(IllegalArgumentException e) {
        AbstractResponse resp = new AbstractResponse();
        return RespUtils.fail(resp, GkFoodCodeEnum.ARGS_ERROR);
    }
}